package ui
{
	import com.greensock.*;
	import com.greensock.easing.*;
	import flash.display.*;
	import flash.media.*;
	import flash.events.*;
	import flash.text.StyleSheet;
	import flash.ui.Mouse;
	//importing classes
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.AntiAliasType;
	import flash.filters.*;
	import flash.net.*;
	import flash.utils.*;
	import flash.geom.*;
	import flash.ui.*;
	//import caurina.transitions.Tweener;
	//import caurina.transitions.properties.DisplayShortcuts;
	//import caurina.transitions.properties.FilterShortcuts;
	
	
	public class Scroller extends MovieClip
	{
		private var source:DisplayObject = null;
		
		private var i:int = 0; //for the 'for' instruction
		
		private var w:int = 0;
		private var h:int = 0;
		
		//global vars
		private var style:String = "";
		private var text:String = "";
		private var scrollbar:String = "";
		
		//scrolling vars
		private var viewIndex:int = 0;
		private var scrollIndex:int = 0;
		
		//offset at which scorlling allows
		private var scrollOffest:int = 40;
		
		
		private var blurScroll:String = "on";//choose on/off if you want blur scroll
		private var mouseDown:Boolean = false;
		
		//asset asset to be scrolled and asset height
		private var asset:DisplayObject = null;
		private var assetHeight:int = 0;
		
		//for css loading
		private var loader:URLLoader = new URLLoader();
		private var _mc:MovieClip;
		public function Scroller(mc:MovieClip)
		{
			_mc = mc;
			addChild(_mc);
			//DisplayShortcuts.init();
			//FilterShortcuts.init();
			visible = false;
		}
		
		public function setupText(ptext:String, pw:int, ph:int,pscrollbar:String, pblurScroll:String, pstyle:String):void
		{
			//function which handles the text
			//_________________________________________________
			
			//globalizing the vars
			w = pw;
			h = ph;
			text = ptext;
			blurScroll = pblurScroll; 
			scrollbar = pscrollbar;
			style = pstyle;
			
			//adding the mask
			var bg:Sprite = new Sprite();
				bg.graphics.beginFill(0x000000);
				bg.graphics.drawRect(0, 0, w, h);
				addChild(bg);
			
			mask = bg;
			if (pstyle != '')
			{
					//if a style sheet is present we load it and then setup the text
				   loader.addEventListener(Event.COMPLETE, setupTextComplete);
				   loader.load(new URLRequest(pstyle) );
			}
			else
			setupTextComplete(null);
			
		}
		public function setupTextComplete(e:Event):void
		{
			//when style sheet is ready
			if (style != '')
			{
				var styleSheet:StyleSheet = new StyleSheet();
				styleSheet.parseCSS(e.target.data);
				_mc.text_txt.styleSheet = styleSheet;
			}
			_mc.text_txt.htmlText = text;
			_mc.text_txt.autoSize = TextFieldAutoSize.LEFT;
			_mc.text_txt.width = w - _mc.scrollbar_mc.width - 5;
			_mc.text_txt.mouseWheelEnabled = false;
			asset = _mc.text_txt;
			assetHeight = _mc.text_txt.textHeight + 5;
			
			visible = true;
			if (_mc.text_txt.textHeight <= h)
			{
				//if the height is enought to show the full text there's no need for this classs
				_mc.text_txt.width = w;
				_mc.scrollbar_mc.visible = false;
			}
			else
			{
				
				if (scrollbar == "on")
				{
					//if a scrollbar is present we set it up
			_mc.scrollbar_mc.x = w - _mc.scrollbar_mc.width;
			_mc.scrollbar_mc.bg_mc.height = h;
			_mc.scrollbar_mc.mouseChildren = false;
			_mc.scrollbar_mc.buttonMode = true;
			_mc.scrollbar_mc.head_mc.height = h / (_mc.text_txt.textHeight) * h;
			//scrollbar functions
			_mc.scrollbar_mc.addEventListener(MouseEvent.MOUSE_DOWN, handleTextMouse)
			stage.addEventListener(MouseEvent.MOUSE_UP, handleTextMouse);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, handleTextWheel);
			
			
				}
				else
				{
					
					_mc.text_txt.width = w;
					_mc.scrollbar_mc.visible = false;
				}
				addEventListener(Event.ENTER_FRAME, handleTextFrame);
				stage.addEventListener(Event.MOUSE_LEAVE, handleMouseLeave);
			}
		}
		public function handleTextMouse(e:MouseEvent):void
		{
			//function which gets the scrollbars attention
			if (e.type == "mouseDown")
			{
				mouseDown = true;
			}
			if (e.type == "mouseUp")
			{
				mouseDown = false;
				TweenMax.to(_mc.scrollbar_mc.head_mc, 1, { frame:1 } );
				TweenMax.to(asset, 0.3, {blurFilter:{blurY:0}});
				//Tweener.addTween(_mc.scrollbar_mc.head_mc, { _frame:1, time:1 } );
				//Tweener.addTween(asset, { _Blur_blurY:0, time:0.3 } );
				
			}
		}
		public function handleTextFrame(e:Event):void
		{
			if (scrollbar == "on")
			{
				//we manage the scrolling
			if (mouseDown == true)
			{
				scrollIndex = _mc.scrollbar_mc.mouseY - (_mc.scrollbar_mc.head_mc.height>>1);
				if (scrollIndex < 0) scrollIndex = 0;
				if (scrollIndex > h - _mc.scrollbar_mc.head_mc.height) scrollIndex = h - _mc.scrollbar_mc.head_mc.height;
				TweenMax.to(_mc.scrollbar_mc.head_mc, 0.5, { y:scrollIndex} );
				TweenMax.to(_mc.scrollbar_mc.head_mc, 1, { frame:30} );
				//Tweener.addTween(_mc.scrollbar_mc.head_mc, { y:scrollIndex, time:0.5 } );
				//Tweener.addTween(_mc.scrollbar_mc.head_mc, { _frame:30, time:1 } );
				
				
				viewIndex = (scrollIndex / (h - _mc.scrollbar_mc.head_mc.height)) * -(assetHeight - h);
				//Tweener.addTween(asset, { y:viewIndex, time:1 } );
				TweenMax.to(asset, 1, { y:viewIndex} );
				if (blurScroll == "on")
				{
					
					TweenMax.to(asset, 0.3, {blurFilter:{blurY:Math.abs(viewIndex-asset.y)/5}});
					//Tweener.addTween(asset, { _Blur_blurY:Math.abs(viewIndex-asset.y)/5, time:0.3 } );
				
				}
				
			}
			}
			else
			{
				if (mouseY >= 0 && mouseY <= h && mouseX >= 0 && mouseX <= w)
				{
					//no bar scrolling
					//________________
				viewIndex = (mouseY / h) * -(assetHeight - h + scrollOffest * 2) + scrollOffest;
				if (viewIndex > 0) viewIndex = 0;
				if (viewIndex < -(assetHeight-h)) viewIndex = -(assetHeight-h);
				TweenMax.to(asset, 0.3, { y:viewIndex,roundProps:["y"],ease:Sine.easeOut } );
				//Tweener.addTween(asset, { y:viewIndex, transition:"easeOutSine", rounded:true, time:.3 } );
				
				if (blurScroll == "on")
				{
					//Tweener.addTween(asset, { _Blur_blurY:Math.abs(viewIndex-asset.y)/5, time:0.3 } );
					TweenMax.to(asset, 0.3, { blurFilter:{blurY:Math.abs(viewIndex-asset.y)/5} } );
				}
				}
				else
				{
					//Tweener.addTween(asset, { _Blur_blurY:0, time:0.3 } );
					TweenMax.to(asset, 0.3, { blurFilter:{blurY:0} } );
				}
			}
		}
		private function handleTextWheel(e:MouseEvent):void
		{
			//mouse wheel function 
			scrollIndex -= e.delta;
				if (scrollIndex < 0) scrollIndex = 0;
				if (scrollIndex > h - _mc.scrollbar_mc.head_mc.height) scrollIndex = h - _mc.scrollbar_mc.head_mc.height;
				TweenMax.to(_mc.scrollbar_mc.head_mc, 0.5, {y:scrollIndex} );
				//Tweener.addTween(_mc.scrollbar_mc.head_mc, { y:scrollIndex, time:0.5 } );
				
				
				viewIndex = (scrollIndex / (h - _mc.scrollbar_mc.head_mc.height)) * -(assetHeight - h);
				//Tweener.addTween(asset, { y:viewIndex, time:1 } );
				TweenMax.to(asset,1, {y:viewIndex} );
				
		}
		public function setupDO(passet:DisplayObject, pw:int, ph:int,pscrollbar:String, pblurScroll:String):void
		{
			//globalizing vars
			w = pw;
			h = ph;
			
			blurScroll = pblurScroll; 
			scrollbar = pscrollbar;
			asset = passet;
			assetHeight = asset.height;
			
			_mc.addChild(asset);
			
			
			// setting mask
			var bg:Sprite = new Sprite();
				bg.graphics.beginFill(0x000000);
				bg.graphics.drawRect(0, 0, asset.width+_mc.scrollbar_mc.width, h);
				addChild(bg);
			
			mask = bg;
			
			
			visible = true;
			if (scrollbar == "on")
			{
			_mc.scrollbar_mc.x = asset.width;
			_mc.scrollbar_mc.bg_mc.height = h;
			_mc.scrollbar_mc.mouseChildren = false;
			_mc.scrollbar_mc.buttonMode = true;
			_mc.scrollbar_mc.head_mc.height = h / assetHeight * h;
			//scrollbar function
			_mc.scrollbar_mc.addEventListener(MouseEvent.MOUSE_DOWN, handleTextMouse)
			stage.addEventListener(MouseEvent.MOUSE_UP, handleTextMouse);
			addEventListener(MouseEvent.MOUSE_WHEEL, handleTextWheel);
			stage.addEventListener(Event.MOUSE_LEAVE, handleMouseLeave);
			
			}
				else
				{
					
					
					_mc.scrollbar_mc.visible = false;
				}
				addEventListener(Event.ENTER_FRAME, handleTextFrame);
		}
		private function handleMouseLeave(e:Event):void
		{
			//we manage mouse leaving the area
			mouseDown = false;
				TweenMax.to(_mc.scrollbar_mc.head_mc, 1, { frame:1 } );
				TweenMax.to(asset, 0.3, { blurFilter:{blurY:0} } );
				//Tweener.addTween(_mc.scrollbar_mc.head_mc, { _frame:1, time:1 } );
				//Tweener.addTween(asset, { _Blur_blurY:0, time:0.3 } );
		}
		
		
		


	}
}